home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / accounts / acct-1.3 / acct-1 / acct-1.3.73 / utils / dumpacct.c < prev    next >
C/C++ Source or Header  |  1995-07-09  |  3KB  |  105 lines

  1. /*
  2.  * dumpacct.c 
  3.  *
  4.  * A small program to print out what is stored in /var/adm/acct file
  5.  *
  6.  * This is released under GPL.
  7.  *
  8.  * 
  9.  * Usage: dumpacct [acct_file]
  10.  * If no file is given, dumpacct reads from stdin.
  11.  *
  12.  * Output format:
  13.  *
  14.  *  ac->ac_comm     tty   uid   gid  flag  exit utime stime ctime
  15.  *  accton        65535     0     0     2     0 0.00 0.00 Sun Jul  9 05:18:39
  16.  *
  17.  * Fields are:
  18.  * ac->ac_comm   name of executed command
  19.  * tty           tty in number format; 8 most significant bits
  20.  *               represent major number and 8 least significant bits
  21.  *               represent minor number. 65535 as tty means that
  22.  *               command didn't have a controlling tty
  23.  * uid           user's effective user id
  24.  * gid           user's effective group id
  25.  * flag          flag bits. 1: process forked child(s), 2: process
  26.  *               used superuser privileges, 4 process dumped core,
  27.  *               8 process were killed by a signal.
  28.  * exit          process' exit status
  29.  * utime         process' execution time in user space in seconds
  30.  * stime         process' execution time in kernel space in seconds
  31.  * ctime         process' startup time (wall clock time)
  32.  *
  33.  *
  34.  * Author: Juha Virtanen, <juha.virtanen@hut.fi>
  35.  * Last time modified on 9th July 1995
  36.  */
  37.  
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #include <time.h>
  42. #include <sys/types.h>
  43. #include <linux/acct.h>
  44. #include <unistd.h>
  45.  
  46. int main (int argc, char *argv[])
  47. {
  48.   struct acct ac[sizeof (struct acct)];
  49.   FILE *fp;
  50.   int fd;
  51.   char filename[FILENAME_MAX+1];
  52.  
  53.   if (argc == 1)
  54.    {
  55.      if ((fd = dup (0)) < 0)
  56.       {
  57.     fprintf (stderr, "dup error");
  58.     exit (1);
  59.       }
  60.      if ((fp = fdopen (fd, "rb")) == NULL)
  61.       {
  62.     if (ferror (fp))
  63.      {
  64.        fprintf (stderr, "could not reopen stdin");
  65.        exit (1);
  66.      }
  67.       }
  68.    }
  69.   else
  70.    {
  71.      strcpy (filename, argv[1]);
  72.      if ((fp = fopen (filename, "rb")) == NULL)
  73.       {
  74.     if (ferror (fp))
  75.      {
  76.        fprintf (stderr, "could not open %s", filename);
  77.        exit (1);
  78.      }
  79.       }
  80. /*     fprintf (stderr, "opened %s\n", filename); */
  81.    }
  82.  
  83.   fprintf (stdout, "%-*.*s %5s %5s %5s %5s %5s %5s %5s %.19s\n", 
  84.        ((int) sizeof (ac->ac_comm)),
  85.        ((int) sizeof (ac->ac_comm)), "ac->ac_comm",
  86.        "tty", "uid", "gid",
  87.        "flag", "exit",
  88.        "utime", "stime",
  89.        "ctime");
  90.  
  91.   while (fread ((void *) ac, sizeof (struct acct), 1, fp))
  92.    {
  93.      fprintf (stdout, "%-*.*s %5d %5hd %5hd %5d %5ld %5.2f %5.2f %.19s\n", 
  94.           ((int) sizeof (ac->ac_comm)), 
  95.           ((int) sizeof (ac->ac_comm)), ac->ac_comm,
  96.           ac->ac_tty, ac->ac_uid, ac-> ac_gid,
  97.           (int) ac->ac_flag, ac->ac_exitcode,
  98.           (double) ac->ac_utime / (double) AHZ,
  99.           (double) ac->ac_stime / (double) AHZ,
  100.           ctime(&ac->ac_btime)); 
  101.    }
  102.  
  103.   return 0;
  104. }
  105.